package com.system.connect.springboot_flowers.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.system.connect.springboot_flowers.entity.Flowercomment;
import com.system.connect.springboot_flowers.service.IFlowercommentService;
import com.system.connect.springboot_flowers.utils.R;
import com.system.connect.springboot_flowers.utils.TokenUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 花种评论表 前端控制器
 * </p>
 *
 * @author yang
 * @since 2025-01-21
 */
@RestController
@RequestMapping("/api/flowercomment/{flowerId}")
public class FlowercommentController {

    private static final Logger log = LoggerFactory.getLogger(FlowercommentController.class);
    @Autowired
    private IFlowercommentService flowerCommentService;

    // 获取指定花种的所有评论
    @GetMapping
    public R getComments(@PathVariable("flowerId") Integer flowerId) {
        System.out.println("这是获取花种的评论");
        QueryWrapper<Flowercomment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flower_id", flowerId);
        return R.success(flowerCommentService.list(queryWrapper));
    }

    // 添加新评论
    @PostMapping
    public R addComment(@PathVariable("flowerId") Integer flowerId, @RequestBody String Commentcontent) {
        // 设置花种ID到评论对象
        try {
            Commentcontent = Commentcontent.substring(1, Commentcontent.length() - 1);
            Flowercomment flowerComment = new Flowercomment();
            flowerComment.setFlowerId(flowerId);
            flowerComment.setContent(Commentcontent);
            System.out.println(Commentcontent);
            flowerComment.setUserId(Integer.valueOf(TokenUtils.getUser()));
            flowerCommentService.save(flowerComment);
            return R.success("评论成功");
        }catch (Exception e){
            return R.error("评论失败");
        }
    }


}

